﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.Cast"/>.
    /// </summary>
    public class CastVM : PreUnaryOperatorVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(Cast),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new CastVM((Cast)codeObject, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ExpressionVM _typeVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.Cast"/>.
        /// </summary>
        public CastVM(Cast cast, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(cast, dictionary)
        {
            _typeVM = (ExpressionVM)CreateVM(cast.Type, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.Cast"/> model.
        /// </summary>
        public Cast Cast
        {
            get { return (Cast)CodeObject; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        protected override Brush RenderBrush(RenderFlags flags)
        {
            // If there is an unresolved operator overload, render it as an error (or warning if in a doc comment)
            return (UnaryOperator.HiddenRef is UnresolvedRef ? (flags.HasFlag(RenderFlags.InDocComment) ? WARNING_BRUSH : ERROR_BRUSH) : PUNC_BRUSH);
        }

        protected override void RenderOperator(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            Brush brush = RenderBrush(flags);
            renderer.RenderText(Cast.ParseTokenStart, brush, this);
            if (_typeVM != null)
                _typeVM.Render(renderer, passFlags);
            renderer.RenderText(Cast.ParseTokenEnd, brush, this);
        }

        public override void UnRender()
        {
            if (_typeVM != null)
                _typeVM.UnRender();
            base.UnRender();
        }

        #endregion
    }
}
